<template>
  <div class="">
    <div class="page-filter mb10 fl">
      <el-form ref="filter" :model="filter" label-width="0">
        <el-form-item class="mr10 mb5 fl">
          <ProFilter v-model="filter.id" />
        </el-form-item>
        <el-form-item class="mr10 mb5 fl" prop="begin_order_time">
          <el-date-picker
            v-model="filter.begin_order_time"
            placeholder="开始时间"
            type="date"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            size="small"
            clearable
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item class="mr10 mb5 fl" prop="end_order_time">
          <el-date-picker
            v-model="filter.end_order_time"
            placeholder="结束时间"
            type="date"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            size="small"
            clearable
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item class="mr10 mb5 fl" style="width: 300px">
          <el-button size="small" @click="app.resetFields($refs['filter'].$parent,'filter')">清 空</el-button>
          <el-button type="primary" size="small" @click="getList">查 询</el-button>
          <el-button type="primary" size="small" @click="handleOut">导 出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table v-loading="app.loading" class="search-table" :data="list" border fit highlight-current-row style="width: 100%;">
      <el-table-column label="序号" align="center" width="56px">
        <template slot-scope="{$index}">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="PO单号" width="190px">
        <template slot-scope="{row}">
          {{ 'PO'+row.id }}
        </template>
      </el-table-column>
      <el-table-column label="客户名称" width="220px" prop="lc_name" />
      <el-table-column label="项目名称" width="220px" :formatter="paytypeFormatter" />
      <el-table-column label="订单金额" align="right" width="140px">
        <template slot-scope="{row}">
          {{ app.toFix(row.trading_price, 2) }}
        </template>
      </el-table-column>
      <el-table-column label="付款方式" min-width="260px">
        <template slot-scope="{row}">
          <span v-for="(item, index) in row.xshks" :key="index" class="mr10">
            <a
              :title="'总金额（'+item.t+'）| '+item.v+'('+item.m+')'"
              href="javascript:void(0)"
              :style="'color:' + (item.t > item.m ? 'green':'red')"
              @click="handleDialog({...{ 'pro_id': row.id, 'paytype': item.i, 'trading_price': row.trading_price }, ...
              {dRef: 'dialog', dTitle: '项目回款信息', dType: 'edit', dField: dField}})"
            >{{ item.v }}</a>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="回款金额" align="right" width="140px">
        <template slot-scope="{row}">
          <a
            href="javascript:void(0)"
            :style="'color:' + (row.trading_price > row.xshk_money ? 'green':'red')"
            @click="handleDialog({...{ 'pro_id': row.id, 'paytype': '', 'trading_price': row.trading_price }, ...
            {dRef: 'dialog', dTitle: '项目回款信息', dType: 'edit', dField: dField}})"
          >{{ app.toFix(row.xshk_money, 2) }}</a>
        </template>
      </el-table-column>
      <el-table-column label="回款比例" align="right" width="110px">
        <template slot-scope="{row}">
          {{ app.toFix((row.xshk_money/row.trading_price)*100, 2) + '%' }}
        </template>
      </el-table-column>
      <el-table-column label="备注" min-width="260px" prop="name" />
      <el-table-column label="操作" align="center" width="90px" fixed="right">
        <template slot-scope="{row}">
          <el-dropdown>
            <el-button type="primary" size="mini">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                @click.native="handleDialog({...{ 'pro_id': row.id, 'paytype': '', 'trading_price': row.trading_price }, ...
                {dRef: 'dialog', dTitle: '项目回款信息', dType: 'edit', dField: dField}})"
              >
                回 款
              </el-dropdown-item>
              <el-dropdown-item
                @click.native="files({e_id: row.id, type: '项目回款',
                                      db_type: 'logc_pro_archives'})"
              >
                附 件
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="filter.pageAll>0" :total="filter.pageAll" :page.sync="filter.pageCurrent" :limit.sync="filter.pageNumber" @pagination="getList" />
    <Dialog ref="dialog" :get-data="getList" :info="dialog" />
    <Cos ref="cos" :info="dialog" />
  </div>
</template>

<script>
import { proOrderXshks, outPros } from '@/api/project'
import Dialog from './components/xshks-dialog'
import ProFilter from '@/components/ProFilter'
import page from '@/utils/page'

export default {
  name: '',
  components: { Dialog, ProFilter },
  extends: page,
  props: {
    reqList: {
      default: proOrderXshks,
      type: Function
    }
  },
  data() {
    return {
      parameter: { state: 1 },
      dField: ['pro_id', 'paytype', 'trading_price']
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleOut() {
      outPros({
        ...this.filter
      }).then(response => {
        this.out(response)
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    paytypeFormatter(e, c) {
      const xshk = e.xshk
      const xshks = []
      if (xshk) {
        const array = xshk.split(',')
        if (array.length > 0) {
          array.forEach(element => {
            const js = element.split('-')
            const f = {}
            f.v = js[0]
            f.i = js[1]
            f.k = js[2]
            f.t = js[3]
            f.m = js[4]
            xshks.push(f)
          })
        }
      }
      e.xshks = xshks
      if (!e.xshk_money || e.xshk_money === '')e.xshk_money = 0
      return e.name
    }
  }
}
</script>
